﻿using System;
using System.Configuration;

namespace Facebook.Web
{
    /// <summary>
    /// This class is responsible for access Facebook configuration information
    /// stored in the web.config file of your web site. 
    /// 
    /// </summary>
    public class Config : ConfigurationSection 
    {
        #region ~~~~~~~~~~ REQUIRED APPLICATION CONFIG OPTIONS ~~~~~~~~~~
        /*
         * This section contains the properties used to access
         * YOUR application key and secret key from the web.config
         * file.
         * 
         * These are REQUIRED
         */

        [ConfigurationProperty("APPKEY", IsRequired = true)]
        public string ApplicationKey
        {
            get { return this["APPKEY"] as string; }
        }

        [ConfigurationProperty("APPNAME", IsRequired = true)]
        public string ApplicationName
        {
            get { return this["APPNAME"] as string; }
        }

        [ConfigurationProperty("SECRETKEY", IsRequired = true)]
        public string SecretKey
        {
            get { return this["SECRETKEY"] as string; }
        }
        #endregion

        #region ~~~~~~~~~~ ADDITIONAL CONFIG OPTIONS ~~~~~~~~~~
        /*
         * Helper config options are here as additional configuration options
         * that are separate from the Facebook options passed in through the 
         * query string.
         */

        private readonly bool useSession = true;

        /// <summary>
        /// Indicates wheather or not to use an HttpSession when in IFrame mode.
        /// If not you'll use a HttpCookie to store facebook data.
        /// </summary>
        [ConfigurationProperty("USESESSION", IsRequired = false)]
        public bool UseSession
        {
            get { return String.IsNullOrEmpty(this["USESESSION"] as string) ? useSession : bool.Parse(this["USESESSION"] as string); }
        }

        private readonly bool autoAddApp = true;
        [ConfigurationProperty("AUTO_ADD_APP", IsRequired = false)]
        public bool AutoAddApp
        {
            get
            {
                return
                    String.IsNullOrEmpty(this["AUTO_ADD_APP"] as string) ? autoAddApp : bool.Parse(this["AUTO_ADD_APP"] as string);
            }
        }

        private readonly bool autoRetry = true;
        [ConfigurationProperty("AUTO_RETRY", IsRequired = false)]
        public bool AutoRetry
        {
            get
            {
                return
                    String.IsNullOrEmpty(this["AUTO_RETRY"] as string) ? autoRetry : bool.Parse(this["AUTO_RETRY"] as string);
            }
        }

        /// <summary>
        /// Default Application URL value.
        /// </summary>
        private readonly string appUrl = @"http://apps.facebook.com/";
        /// <summary>
        /// The facebook app url (including your application name
        /// This is not required we have a default set in code,
        /// but just in case facebook changes it we'll add the config
        /// option to override the default.
        /// </summary>
        [ConfigurationProperty("APPURL", IsRequired = false)]
        public string AppUrl
        { get { return String.IsNullOrEmpty(this["APPURL"] as string) ? appUrl + ApplicationName : (this["APPURL"] as string) + ApplicationName; } }

        /// <summary>
        /// Default Add URL value. (Application Key is added for you.)
        /// </summary>
        private readonly string addUrl = @"http://www.facebook.com/add.php?api_key=";
        /// <summary>
        /// The facebook add url. 
        /// This is not required we have a default set in code,
        /// but just in case facebook changes it we'll add the config
        /// option to override the default.
        /// </summary>
        [ConfigurationProperty("ADDURL", IsRequired = false)]
        public string AddUrl
        { get { return String.IsNullOrEmpty(this["ADDURL"] as string) ? addUrl + ApplicationKey : (this["ADDURL"] as string) + ApplicationKey; } }

        /// <summary>
        /// Default Login URL value. (Application Key is added for you.)
        /// </summary>
        private readonly string loginUrl = @"http://www.facebook.com/login.php?api_key=";
        /// <summary>
        /// The facebook login url.
        /// This is not required because we have a default set in code.
        /// We've added logic to check the web.config just in case we
        /// need to override or if FB changes the location.
        /// </summary>
        [ConfigurationProperty("LOGINURL", IsRequired = false)]
        public string LoginUrl
        { get { return String.IsNullOrEmpty(this["LOGINURL"] as string) ? loginUrl + ApplicationKey : (this["LOGINURL"] as string) + ApplicationKey; } }

        private readonly string canvasParam = "&canvas";
        [ConfigurationProperty("CANVASPARAM", IsRequired = false)]
        public string CanvasParam
        { get { return String.IsNullOrEmpty(this["CANVASPARAM"] as string) ? canvasParam : this["CANVASPARAM"] as string; } }

        #endregion

        #region ~~~~~~~~~~ SESSION KEY NAMES CONFIG ~~~~~~~~~
        /*
         * This section contains the properties we'll use to retreive
         * the session key names from the web.config file if we choose
         * to put them in the web.config.
         * 
         * SK = SESSION KEY
         * 
         */

        private readonly string _skSessionKey = "SessionKey";
        [ConfigurationProperty("SK_SESSION_KEY", IsRequired = false)]
        public string skSessionKey
        {
            get { return String.IsNullOrEmpty(this["SK_SESSION_KEY"] as string) ? _skSessionKey : this["SK_SESSION_KEY"] as string; }
        }

        /// <summary>
        /// Default value of the 
        /// </summary>
        private readonly string _skUserId = "UserId";
        [ConfigurationProperty("SK_USER_ID", IsRequired = false)]
        public string skUser
        {
            get { return String.IsNullOrEmpty(this["SK_USER_ID"] as string) ? _skUserId : this["SK_USER_ID"] as string; }
        }

        #endregion

        #region ~~~~~~~~~~ QUERYSTRING KEY NAMES CONFIG ~~~~~~~~~~
        /*
         * This section contains the properties we'll use to retreive
         * the query string names from the web.config file if we choose
         * to put them in the web.config.
         * 
         * QSK = QUERYSTRING KEY
         * 
         * Some Facebook querystring keys include: 
         * fb_sig_in_iframe
         * fb_sig_in_canvas
         * fb_sig_time
         * fb_sig_added 
         * fb_sig_user
         * fb_sig_profile_update_time
         * fb_sig_session_key
         * fb_sig_expires
         * fb_sig_api_key
         * fb_sig
         * auth_token
         */

        private readonly string _qskInFrame = "fb_sig_in_iframe";
        [ConfigurationProperty("QSK_IN_IFRAME", IsRequired = false)]
        public string qskInFrame
        {
            get { return String.IsNullOrEmpty(this["QSK_IN_IFRAME"] as string) ? _qskInFrame : this["QSK_IN_IFRAME"] as string; }
        }

        private readonly string _qskInCanvas = "fb_sig_in_canvas";
        [ConfigurationProperty("QSK_IN_CANVAS", IsRequired = false)]
        public string qskInCanvas
        {
            get { return String.IsNullOrEmpty(this["QSK_IN_CANVAS"] as string) ? _qskInCanvas : this["QSK_IN_CANVAS"] as string; }
        }

        private readonly string _qskTime = "fb_sig_time";
        [ConfigurationProperty("QSK_TIME", IsRequired = false)]
        public string qskTime
        {
            get { return String.IsNullOrEmpty(this["QSK_TIME"] as string) ? _qskTime : this["QSK_TIME"] as string; }
        }

        private readonly string _qskAdded = "fb_sig_added";
        [ConfigurationProperty("QSK_ADDED", IsRequired = false)]
        public string qskAdded
        {
            get { return String.IsNullOrEmpty(this["QSK_ADDED"] as string) ? _qskAdded : this["QSK_ADDED"] as string; }
        }

        private readonly string _qskUser = "fb_sig_user";
        [ConfigurationProperty("QSK_USER", IsRequired = false)]
        public string qskUser
        {
            get { return String.IsNullOrEmpty(this["QSK_USER"] as string) ? _qskUser : this["QSK_USER"] as string; }
        }

        private readonly string _qskProfileUdateTime = "fb_sig_profile_update_time";
        [ConfigurationProperty("QSK_PROFILE_UPDATE_TIME", IsRequired = false)]
        public string qskProfileUpdateTime
        {
            get { return String.IsNullOrEmpty(this["QSK_PROFILE_UPDATE_TIME"] as string) ? _qskProfileUdateTime : this["QSK_PROFILE_UPDATE_TIME"] as string; }
        }

        private readonly string _qskSessionKey = "fb_sig_session_key";
        [ConfigurationProperty("QSK_SESSION_KEY", IsRequired = false)]
        public string qskSessionKey
        {
            get { return String.IsNullOrEmpty(this["QSK_SESSION_KEY"] as string) ? _qskSessionKey : this["QSK_SESSION_KEY"] as string; }
        }

        private readonly string _qskExpires = "fb_sig_expires";
        [ConfigurationProperty("QSK_EXPIRES", IsRequired = false)]
        public string qskExpires
        {
            get { return String.IsNullOrEmpty(this["QSK_EXPIRES"] as string) ? _qskExpires : this["QSK_EXPIRES"] as string; }
        }

        private readonly string _qskApiKey = "fb_sig_api_key";
        [ConfigurationProperty("QSK_API_KEY", IsRequired = false)]
        public string qskApiKey
        {
            get { return String.IsNullOrEmpty(this["QSK_API_KEY"] as string) ? _qskApiKey : this["QSK_API_KEY"] as string; }
        }

        private readonly string _qskAuthToken = "auth_token";
        [ConfigurationProperty("QSK_AUTH_TOKEN", IsRequired = false)]
        public string qskAuthToken
        { get { return String.IsNullOrEmpty(this["QSK_AUTH_TOKEN"] as string) ? _qskAuthToken : this["QSK_AUTH_TOKEN"] as string; } }

        #endregion
    }

}
